package com.itheima.dao;

import com.itheima.pojo.Blog;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

/**
 * @ClassName BlogMapper
 * @Description 博客表
 * @Author pengkaka
 * @Date 2020/8/4 10:50
 * @Version 1.0
 **/
public interface BlogMapper extends Mapper<Blog> {

    // 根据标签id查询数量
    @Select("SELECT COUNT(*) count, tag_uid FROM `t_blog` GROUP BY tag_uid ")
    List<Map<String,Object>>findTagCount();

    // 获取一年内的文章贡献数
    @Select("SELECT DISTINCT DATE_FORMAT(create_time, '%Y-%m-%d') DATE, COUNT(uid) COUNT FROM t_blog WHERE 1=1 && status = 1 && create_time >= #{startTime} && create_time < #{endTime} GROUP BY DATE_FORMAT(create_time, '%Y-%m-%d')")
    List<Map<String, Object>> getBlogContributeCount(@Param("startTime") String startTime, @Param("endTime") String endTime);
    
    // 通过分类获取博客数量
    @Select("SELECT blog_sort_uid, COUNT(blog_sort_uid) count FROM t_blog where status = 1 GROUP BY blog_sort_uid")
    List<Map<String,Object>> getBlogCountByBlogSort();
    
    // 通过标签获取博客数量
    @Select("SELECT tag_uid, COUNT(tag_uid) count FROM t_blog where status = 1 GROUP BY tag_uid")
    List<Map<String, Object>> getBlogCountByTag();

    //通过Uid获取博客Content内容
    @Select("SELECT content FROM `t_blog` WHERE uid = #{uid}")
    String getBlogByUid(String uid);

    //通过Uid获取博客收藏数
    @Select("SELECT collect_count FROM `t_blog` WHERE uid = #{uid}")
    Integer getBlogPraiseCountByUid(String uid);

    //通过Uid获取BlogSortUid
    @Select("SELECT blog_sort_uid FROM `t_blog` WHERE uid = #{uid}")
    String getBlogSortUid(String uid);

    //通过BlogSortUid获取博客Content内容的集合
    @Select("SELECT content FROM `t_blog` WHERE blog_sort_uid = #{blogSortUid}")
    List<String> getBlogContentList(String blogSortUid);

    //通过uid给博客点赞
    @Update("UPDATE `t_blog` SET collect_count = collect_count + 1  WHERE uid = #{uid};")
    void updateCollectCountByUid(String uid);
}
